package com.javagroup.chessonline.view;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.javagroup.chessonline.client.Client;
import com.thuan.hibernate.Utils;

public class RegisterForm extends ChessOnlineView{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1071567726635351723L;
	
	private JLabel mErrorLbl;
	private JTextField mUsernameTF;
	private JPasswordField mPasswordTF;
	private JTextField mNameTF;
	
	public static final String TIPS_MESSAGE = "<html>The Username (contains 6 - 60 characters) must contain A-Z, 0-9 " +
			" <br>and the Password (contains 8 - 32 characters) must contain at least one digit and one upper character!</html>";
	public static final String INVALID_USER_MESSAGE = "<html>User Existed!</html>";
	public static final String INVALID_NAME = "Please fill the name!";
	
	
	public RegisterForm(Client mainScreen) {
		super(mainScreen);
	
		// Create form with 2 text field and Login button
		this.setSize(500, 250);
		
		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel usernameLbl = new JLabel("Username");
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		add(usernameLbl, c);
		mUsernameTF = new JTextField(60);
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		add(mUsernameTF, c);

		JLabel passwordLbl = new JLabel("Password");
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		add(passwordLbl, c);
		mPasswordTF = new JPasswordField(32);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.8;
		c.gridx = 1;
		c.gridy = 2;
		add(mPasswordTF, c);
		
		JLabel nameLbl = new JLabel("Name");
		c.weightx = 0.5;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 3;
		add(nameLbl, c);
		mNameTF = new JTextField(32);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.8;
		c.gridx = 1;
		c.gridy = 3;
		add(mNameTF, c);
		
		JButton registerCmd = new JButton("Register");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 4;
		add(registerCmd, c);
		
		mErrorLbl = new JLabel(TIPS_MESSAGE, SwingConstants.CENTER);
		mErrorLbl.setForeground(Color.red);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 40;      //make this component tall
		c.weightx = 0.0;
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;
		add(mErrorLbl, c);
		
		registerCmd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String username = mUsernameTF.getText();
				String password = String.copyValueOf(mPasswordTF.getPassword());
				String name = mNameTF.getText();
				
				if(!Utils.validUser(username, password)) {
					mErrorLbl.setText(TIPS_MESSAGE);
					return;
				}
				
				if(!Utils.hasValue(name)) {
					mErrorLbl.setText(INVALID_NAME);
					return;
				}
					
				mMainScreen.register(username, password, name);
			}
		});
				
	}

	public void setInvalidUserError() {
		this.mErrorLbl.setText(INVALID_USER_MESSAGE);
	}

	@Override
	public void onShow() {
		this.mErrorLbl.setText(TIPS_MESSAGE);
		this.mUsernameTF.setText("");
		this.mPasswordTF.setText("");
	}

	@Override
	public void onHide() {
		// TODO Auto-generated method stub
		
	}
	
}
